<?php
namespace app\integral\model;

use app\BaseModel;
use app\shop\model\AdminCoupon;
use app\shop\model\AdminShopSpePrice;
use think\facade\Db;

class IntegralGoods extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_integral_shop_goods';



    protected $append = [

        'spe_name'

    ];


    /**
     * @author chenniang
     * @DataTime: 2021-10-29 18:54
     * @功能说明:规格名字
     */
    public function getSpeNameAttr($value,$data){

        if(!empty($data['spe_id'])&&isset($data['goods_type'])){

            if($data['goods_type']==1){

                $spe_model = new AdminShopSpePrice();

                $info = $spe_model->dataInfo(['id'=>$data['spe_id']]);

                if(!empty($info)){

                    return $info['spe_name_text'];
                }
            }else{

                $coupon_model = new AdminCoupon();

                $title = $coupon_model->where(['id'=>$data['spe_id']])->value('title');

                return $title;

            }

        }

    }

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $res = $this->insert($data);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page){

        $data = $this->where($dis)->order('top desc,id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2021-10-29 13:34
     * @功能说明:积分列表可以通过商品名字查询
     */
    public function dataGoodsList($dis,$page=10){

        $data = $this->alias('a')
            ->join('longbing_card_goods b','a.goods_id = b.id','left')
            ->where($dis)
            ->field('a.*,b.name as goods_name')
            ->group('a.id')
            ->order('a.id desc')
            ->paginate($page)
            ->toArray();

        return $data;


    }


    /**
     * @author chenniang
     * @DataTime: 2021-10-29 13:54
     * @功能说明:初始化活动时间
     */
    public function initAtv(){

        $this->where('start_time','>',time())->update(['atv_status'=>1]);

        $this->where('start_time','<',time())->update(['atv_status'=>2]);

        $this->where('end_time','>',time())->update(['atv_status'=>3]);

        return true;
    }


    /**
     * @author chenniang
     * @DataTime: 2022-01-24 13:26
     * @功能说明:修改库存
     */
    public function updateStock($id,$num,$add=1){

        $dis = [

            'id' => $id
        ];

        $data = $this->dataInfo($dis);

        if(!empty($data)){

            $num = $add==1?$num:$num*-1;

            if($add==1&&$data['have_stock']>=$data['stock']){

                return 0;
            }

            $update = [

                'have_stock' => $data['have_stock'] + $num,

                'lock'       => $data['lock']+1

            ];

            $dis['lock'] = $data['lock'];

            $res = $this->dataUpdate($dis,$update);

            return $res;

        }else{

            return 1;
        }

    }








}